package com.atguigu.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;

public class JedisTest {

   static JedisPool jedisPool=initPool();

   static JedisSentinelPool jedisSentinelPool=initSentinelPool();

    private static JedisSentinelPool initSentinelPool() {
        HashSet<String> sentinelSet = new HashSet<>();
        sentinelSet.add("192.168.11.102:26379");
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10000);
        jedisPoolConfig.setMaxIdle(200);
        jedisPoolConfig.setMinIdle(100);
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setMaxWaitMillis(1000);
        jedisPoolConfig.setTestOnBorrow(true);

        JedisSentinelPool sentinelPool = new JedisSentinelPool("mymaster", sentinelSet, jedisPoolConfig);

        return sentinelPool;
    }

    private static JedisPool initPool() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10000);
        jedisPoolConfig.setMaxIdle(200);
        jedisPoolConfig.setMinIdle(100);
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setMaxWaitMillis(1000);
        jedisPoolConfig.setTestOnBorrow(true);

        JedisPool jedisPool= new JedisPool(jedisPoolConfig, "hadoop102", 6379, 60000,"atguigu");

        return jedisPool;
    }

    public static Jedis  getJedisFromPool(){
        return jedisPool.getResource();
    }

    public static Jedis  getJedisFromSentinelPool(){
        return jedisSentinelPool.getResource();
    }

    public static void main(String[] args) {
       // Jedis jedis = new Jedis("hadoop102", 6379);
        Jedis jedis = JedisTest.getJedisFromSentinelPool() ;

      //  jedis.auth("atguigu");
        System.out.println("jedis.ping() = " + jedis.ping());

        jedis.set("k1","v10000");
        System.out.println("jedis.get(\"k1\") = " + jedis.get("k1"));


        jedis.close();// 会自动判断 jedis来源 如果是从池中获得的 则会自动归还到池中
    }


}
